package com.boluo.customer.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.boluo.common.utils.DateUtils;
import com.boluo.common.utils.SecurityUtils;
import com.boluo.customer.domain.CustomerLinkman;
import com.boluo.customer.dto.CustomerInfoDTO;
import com.boluo.customer.mapper.CustomerLinkmanMapper;
import com.boluo.customer.vo.CustomerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.boluo.customer.mapper.CustomerInfoMapper;
import com.boluo.customer.domain.CustomerInfo;
import com.boluo.customer.service.ICustomerInfoService;
import org.springframework.util.Assert;

import javax.annotation.Resource;


/**
 * 客户基本信息Service业务层处理
 *
 * @author SekonoAlpaca
 * @date 2023-05-02
 */
@Service
public class CustomerInfoServiceImpl implements ICustomerInfoService {
    @Resource
    private CustomerInfoMapper customerInfoMapper;
    @Resource
    ICustomerInfoService customerInfoService;

    @Resource
    private CustomerLinkmanMapper customerLinkmanMapper;

    /**
     * 查询客户基本信息
     *
     * @param id 客户基本信息主键
     * @return 客户基本信息
     */
    @Override
    public CustomerInfo selectCustomerInfoById(Long id) {
        return customerInfoMapper.selectCustomerInfoById(id);
    }

    /**
     * 查询客户基本信息列表
     *
     * @param customerInfo 客户基本信息
     * @return 客户基本信息
     */
    @Override
    public List<CustomerInfo> selectCustomerInfoList(CustomerInfo customerInfo) {
        return customerInfoMapper.selectCustomerInfoList(customerInfo);
    }

    /**
     * 新增客户基本信息
     *
     * @param customerInfo 客户基本信息
     * @return 结果
     */
    @Override
    public int insertCustomerInfo(CustomerInfo customerInfo) {
        //参数校验 参数是否存在 参数是否合法 参数和表有无冲突

        /*必填不为空*/
        Assert.notEmpty(Arrays.asList(
                customerInfo.getCustomerName()
                , customerInfo.getLegalLeader()
                , customerInfo.getRegisterDate()
                , customerInfo.getOpenState()
                , customerInfo.getProvince()
                , customerInfo.getRegCapital()
                , customerInfo.getIndustry()
                , customerInfo.getRegAddr()
                , customerInfo.getScope()), "参数不能为空");

        /*企业名字不能重复*/
        customerInfoService.selectCustomerInfoByName(customerInfo.getCustomerName());

        /*企业名称100字内*/
        Assert.state(customerInfo.getCustomerName().length() <= 100, "名称违规");

        /*法定代表人30字内*/
        Assert.state(customerInfo.getLegalLeader().length() <= 30, "代表人名称违规");

        /*注册资本为正数*/
        Assert.state(customerInfo.getRegCapital().compareTo(BigDecimal.ZERO) > 0, "资金异常"
        );
        /*所属行业30字内*/
        Assert.state(customerInfo.getIndustry().length() < 30, "所属行业非法");

        /*注册地址500字内*/
        Assert.state(customerInfo.getRegAddr().length() < 500, "所属行业非法");

        /*经营范围500字内*/
        Assert.state(customerInfo.getScope().length() < 500, "所属行业非法");


        //参数补偿

        /*录入人*/
        customerInfo.setInputUser(SecurityUtils.getUserId());
        /*录入时间*/
        customerInfo.setInputTime(DateUtils.getNowDate());
        /*注册资金保留两位*/
        customerInfo.setRegCapital(
                customerInfo.getRegCapital().setScale(2));


        return customerInfoMapper.insertCustomerInfo(customerInfo);
    }

    /**
     * 修改客户基本信息
     *
     * @param customerInfo 客户基本信息
     * @return 结果
     */
    @Override
    public int updateCustomerInfo(CustomerInfo customerInfo) {
        return customerInfoMapper.updateCustomerInfo(customerInfo);
    }

    /**
     * 批量删除客户基本信息
     *
     * @param ids 需要删除的客户基本信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerInfoByIds(Long[] ids) {
        return customerInfoMapper.deleteCustomerInfoByIds(ids);
    }

    /**
     * 删除客户基本信息信息
     *
     * @param id 客户基本信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerInfoById(Long id) {
        return customerInfoMapper.deleteCustomerInfoById(id);
    }

    /**
     * 通过名称查询数量
     * @param name 名称关键字
     * @return 数量
     */
    @Override
    public int selectCustomerInfoByName(String name) {
        return customerInfoMapper.selectCustomerInfoByName(name);
    }

    /**
     * 通过关键字及经营状态所属地区录入时间查询
     * @param customerInfo 包含搜索条件对象
     * @return 结果
     */
    @Override
    public List<CustomerInfoDTO> selectCustomerInfoListByComplex(CustomerVO customerInfo) {

      return customerInfoMapper.selectCustomerInfoComplex(customerInfo).stream()
              .map(cusInfo->{
                  CustomerInfoDTO cusDTO = new CustomerInfoDTO();
                  BeanUtils.copyProperties(cusInfo,cusDTO);
                  cusDTO.setInputUserName(SecurityUtils.getUsername());
                  return cusDTO;
              }).collect(Collectors.toList());

    }

    /**
     * 查询所属客户列表(不分页)
     * @return
     */
    @Override
    public List<CustomerInfoDTO> listAllByIdAndName() {
        return customerInfoMapper.listAllByIdAndName();
    }

    /**
     * 获取所属客户下的联系人信息
     * @param id
     * @return
     */
    @Override
    public List<CustomerLinkman> selectName(Long id) {
        return customerLinkmanMapper.selectName(id);
    }
}
